package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 * 扣费策略
 * @author YDW
 * projectName:project2202_4
 * time:2022/6/20 22:30
 * description:
 */
@Service("fee")
@Slf4j
public class FeeServiceImpl implements FilterChainService {
    @Autowired
    CacheFeign cacheFeign;

    @Autowired
    PushMsgUtil pushMsgUtil;
    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("[策略模块 -- 扣费策略] 策略开始执行！ submit={}",submit);
        Map clientmap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_CLIENT + submit.getClientID());
        //获取账户余额
        String feestr = String.valueOf(clientmap.get("fee"));
        Integer source = submit.getSource();
        long fee = Long.parseLong(feestr);
        //获取价格
        Long price = submit.getFee();
        if (price==null){
            log.info("[策略模块 -- 扣费策略] 短信发送失败，该通道暂未开通");
            if(source==1){
                pushMsgUtil.pushReport(submit,"短信发送失败，该通道暂未开通！!");
            }
            pushMsgUtil.sendLog(submit,"短信发送失败，该通道暂未开通！!");
            return false;
        }
        if (fee-price>0){
            clientmap.put("fee",fee-price);
            cacheFeign.hMSet(CacheConstants.CACHE_PREFIX_CLIENT + submit.getClientID(),clientmap);
            log.info("[策略模块 -- 扣费策略] 扣费成功！ submit={}",submit);
            return true;
        }else {
            log.info("[策略模块 -- 扣费策略] 扣费失败,余额不足！ submit={}",submit);
            if(source==1){
                pushMsgUtil.pushReport(submit,"扣费失败,余额不足！!");
            }
            pushMsgUtil.sendLog(submit,"扣费失败,余额不足！!");
            return false;
        }

    }
}
